import React, { useImperativeHandle, useState } from 'react';
import { message, Modal } from 'antd';
import { useRequest } from '@/hooks';
import { fetchDuty } from '@/pages/Organization/OrgFlow/services';
import styles from './index.less';

/**
 * 岗位职责的 Modal 组件
 * @param
 * @returns {JSX.Element}
 * @constructor
 */
const JobDutyModal = React.forwardRef((props, ref) => {
    const [visible, setVisible] = useState(false);
    const [jobDutyData, setJobDutyData] = useState({});

    const { run: fetchDutyRun } = useRequest(param => fetchDuty(param), {
        manual: true,
        onSuccess: result => {
            const { code, data, msg } = result;
            if (code === 10000) {
                setVisible(true);
                setJobDutyData(data);
            } else {
                message.error(msg);
            }
        }
    });

    const open = dutyCode => {
        fetchDutyRun({ dutyCode }).then();
    };

    const handlerJobDutyCancel = () => {
        setVisible(false);
        setJobDutyData({});
    };
    useImperativeHandle(ref, () => ({
        open
    }));

    // render
    return (
        <Modal
            title={`${jobDutyData?.dutyAlias ? jobDutyData?.dutyAlias : ''}岗位职责 ${
                jobDutyData?.auditStatus === 1 ? '审批中' : ''
            }`}
            visible={visible}
            className={styles.clearFooterBorder}
            cancelButtonProps={{ style: { display: 'none' } }}
            okButtonProps={{ style: { display: 'none' } }}
            onCancel={handlerJobDutyCancel}
            onOk={handlerJobDutyCancel}
        >
            <div className={styles.jobModalBody}>
                <div
                    style={{ fontSize: 12 }}
                    // eslint-disable-next-line react/no-danger
                    dangerouslySetInnerHTML={{
                        __html: jobDutyData.dutyDesc && jobDutyData.dutyDesc.replace(/(\r\n|\n|\r)/gm, '<br />')
                    }}
                />
            </div>
        </Modal>
    );
});

export default JobDutyModal;
